package com.example.podcaster.util;

import org.holoeverywhere.ArrayAdapter;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.podcaster.R;

public class PodcastListAdapter extends ArrayAdapter<String>
{
    private String[] values;

    private Context context;

    private String[] names;

    private int[] ids;

    private SharedPreferences preferences;

    public PodcastListAdapter(Context context, String[] values, String[] names, int[] ids, SharedPreferences prefs)
    {
        super(context, R.layout.podcast_entry, values);
        this.values = values;
        this.context = context;
        this.names = names;
        preferences = prefs;
        this.ids = ids;
        // TODO Auto-generated constructor stub
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        final int i = position;
        View rowView = inflater.inflate(R.layout.podcast_entry, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.textView);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView);
        setUpRankingButtons(position, names, rowView);
        final ImageView starview = (ImageView) rowView.findViewById(R.id.imageViewStar);
        starview.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                SharedPreferences.Editor editor = preferences.edit();

                if (preferences.contains(String.valueOf(ids[i])))
                {
                    editor.remove(String.valueOf(ids[i]));
                    starview.setImageResource(R.drawable.star_hollow);
                }
                else
                {
                    editor.putInt(String.valueOf(ids[i]), i);
                    starview.setImageResource(R.drawable.star_filled);
                }
                editor.commit();
            }
        });
        if (preferences.contains(String.valueOf(ids[i])))
        {
            starview.setImageResource(R.drawable.star_filled);
        }
        else
        {
            starview.setImageResource(R.drawable.star_hollow);
        }

        textView.setText(names[i]);

        // Change icon based on name
        String s = values[position];

        if (s.equals("audio"))
        {
            imageView.setImageResource(R.drawable.audio_icon);
        }
        else if (s.equals("video"))
        {
            imageView.setImageResource(R.drawable.video_icon);
        }

        return rowView;
    }

    public void setUpRankingButtons(int i, String[] n, View rowView)
    {
        final ImageView[] ratingButtons = new ImageView[5];
        ImageView button1 = (ImageView) rowView.findViewById(R.id.listReviewButton1);
        ratingButtons[0] = button1;
        final int index = i;
        final String[] names = n;
        button1.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                int i = 0;
                SharedPreferences.Editor editor = preferences.edit();
                if (preferences.contains(ids[index] + "_" + names[index] + "_rating"))
                {
                    i = preferences.getInt(ids[index] + "_" + names[index] + "_rating", 0);

                    editor.remove(ids[index] + "_" + names[index] + "_rating");
                    if (i == 1)
                    {
                        editor.putInt(ids[index] + "_" + names[index] + "_rating", 0);
                    }
                    else if (i != 0)
                    {
                        editor.putInt(ids[index] + "_" + names[index] + "_rating", 1);

                    }
                    else
                    {
                        editor.putInt(ids[index] + "_" + names[index] + "_rating", 1);
                    }
                }
                else
                {
                    editor.putInt(ids[index] + "_" + names[index] + "_rating", 1);
                }

                editor.commit();
                resetRatingIcons(index, ratingButtons);
            }
        });
        ImageView button2 = (ImageView) rowView.findViewById(R.id.listReviewButton2);
        button2.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                int i = 0;
                SharedPreferences.Editor editor = preferences.edit();
                if (preferences.contains(ids[index] + "_" + names[index] + "_rating"))
                {
                    i = preferences.getInt(ids[index] + "_" + names[index] + "_rating", 0);

                    editor.remove(ids[index] + "_" + names[index] + "_rating");
                    if (i == 0)
                    {
                        i = 1;
                    }
                    if (ratingButtons[i - 1] == v)
                    {
                        editor.putInt(ids[index] + "_" + names[index] + "_rating", 0);
                    }
                    else
                    {
                        editor.putInt(ids[index] + "_" + names[index] + "_rating", 2);
                    }
                }
                else
                {
                    editor.putInt(ids[index] + "_" + names[index] + "_rating", 2);
                }
                editor.commit();
                resetRatingIcons(index, ratingButtons);
            }
        });
        ratingButtons[1] = button2;
        ImageView button3 = (ImageView) rowView.findViewById(R.id.listReviewButton3);
        button3.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                int i = 0;
                SharedPreferences.Editor editor = preferences.edit();
                if (preferences.contains(ids[index] + "_" + names[index] + "_rating"))
                {
                    i = preferences.getInt(ids[index] + "_" + names[index] + "_rating", 0);

                    editor.remove(ids[index] + "_" + names[index] + "_rating");
                    if (i == 0)
                    {
                        i = 1;
                    }
                    if (ratingButtons[i - 1] == v)
                    {
                        editor.putInt(ids[index] + "_" + names[index] + "_rating", 0);
                    }
                    else
                    {
                        editor.putInt(ids[index] + "_" + names[index] + "_rating", 3);
                    }
                }
                else
                {
                    editor.putInt(ids[index] + "_" + names[index] + "_rating", 3);
                }
                editor.commit();
                resetRatingIcons(index, ratingButtons);
            }
        });
        ratingButtons[2] = button3;
        ImageView button4 = (ImageView) rowView.findViewById(R.id.listReviewButton4);
        button4.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                int i = 0;
                SharedPreferences.Editor editor = preferences.edit();
                if (preferences.contains(ids[index] + "_" + names[index] + "_rating"))
                {
                    i = preferences.getInt(ids[index] + "_" + names[index] + "_rating", 0);

                    editor.remove(ids[index] + "_" + names[index] + "_rating");
                    if (i == 0)
                    {
                        i = 1;
                    }
                    if (ratingButtons[i - 1] == v)
                    {
                        editor.putInt(ids[index] + "_" + names[index] + "_rating", 0);
                    }
                    else
                    {
                        editor.putInt(ids[index] + "_" + names[index] + "_rating", 4);
                    }
                }
                else
                {
                    editor.putInt(ids[index] + "_" + names[index] + "_rating", 4);
                }
                editor.commit();
                resetRatingIcons(index, ratingButtons);
            }
        });
        ratingButtons[3] = button4;
        ImageView button5 = (ImageView) rowView.findViewById(R.id.listReviewButton5);
        button5.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                int i = 0;
                SharedPreferences.Editor editor = preferences.edit();
                if (preferences.contains(ids[index] + "_" + names[index] + "_rating"))
                {
                    i = preferences.getInt(ids[index] + "_" + names[index] + "_rating", 0);

                    editor.remove(ids[index] + "_" + names[index] + "_rating");
                    if (i == 0)
                    {
                        i = 1;
                    }
                    if (ratingButtons[i - 1] == v)
                    {
                        editor.putInt(ids[index] + "_" + names[index] + "_rating", 0);
                    }
                    else
                    {
                        editor.putInt(ids[index] + "_" + names[index] + "_rating", 5);
                    }
                }
                else
                {
                    editor.putInt(ids[index] + "_" + names[index] + "_rating", 5);
                }
                editor.commit();
                resetRatingIcons(index, ratingButtons);
            }
        });
        ratingButtons[4] = button5;
        resetRatingIcons(index, ratingButtons);
    }

    public void resetRatingIcons(int index, ImageView[] ratingButtons)
    {
        int i = 0;
        if (preferences.contains(ids[index] + "_" + names[index] + "_rating"))
        {
            i = preferences.getInt(ids[index] + "_" + names[index] + "_rating", 0);
        }
        for (int j = 0; j < ratingButtons.length; j++)
        {
            if (j < i)
            {
                ratingButtons[j].setBackgroundResource(R.drawable.heart_filled);
            }
            else if (i == 0)
            {
                ratingButtons[j].setBackgroundResource(R.drawable.heart_hollow);
            }
            else
            {
                ratingButtons[j].setBackgroundResource(R.drawable.heart_hollow);
            }
        }
    }

}
